package cn.edu.cqu.bysj.wm.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Set;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;

import cn.edu.cqu.bysj.wm.gui.component.AlgorithmTabPane;
import cn.edu.cqu.bysj.wm.gui.model.AlgorithmConfig;
import java.awt.GridLayout;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public abstract class ConfigUi extends JDialog {
	protected AlgorithmConfig config;
	private final JPanel contentPanel = new JPanel();
	protected MainFrame superParent;
	private JLabel label_1;
	protected JPanel childContent;
	private JPanel valuePanel;
	private JPanel namePanel;
	private JSplitPane splitPane;

	/**
	 * create the config dialog
	 * 
	 * @param parent
	 * @param isModel
	 */
	public ConfigUi(MainFrame parent, boolean isModel) {

		superParent = parent;

		setBounds(100, 100, 900, 416);
		ProgressDialog.setToScreenCenter(this);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JPanel existedContent = new JPanel();
		childContent = new JPanel();
		childContent.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null,
				null, null, null));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(gl_contentPanel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_contentPanel
						.createSequentialGroup()
						.addComponent(childContent, GroupLayout.PREFERRED_SIZE,
								175, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(existedContent, GroupLayout.DEFAULT_SIZE,
								690, Short.MAX_VALUE)));
		gl_contentPanel.setVerticalGroup(gl_contentPanel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_contentPanel
						.createSequentialGroup()
						.addGroup(
								gl_contentPanel
										.createParallelGroup(
												Alignment.TRAILING, false)
										.addComponent(existedContent,
												Alignment.LEADING, 0, 0,
												Short.MAX_VALUE)
										.addComponent(childContent,
												Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE, 302,
												Short.MAX_VALUE))
						.addContainerGap(26, Short.MAX_VALUE)));
		{
			label_1 = new JLabel("已有设置");
		}
		JScrollPane scrollPane_1 = new JScrollPane();
		GroupLayout gl_existedContent = new GroupLayout(existedContent);
		gl_existedContent.setHorizontalGroup(gl_existedContent
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_existedContent.createSequentialGroup()
								.addComponent(label_1)
								.addContainerGap(630, Short.MAX_VALUE))
				.addComponent(scrollPane_1, Alignment.TRAILING,
						GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE));
		gl_existedContent.setVerticalGroup(gl_existedContent
				.createParallelGroup(Alignment.LEADING).addGroup(
						gl_existedContent
								.createSequentialGroup()
								.addComponent(label_1)
								.addGap(5)
								.addComponent(scrollPane_1,
										GroupLayout.DEFAULT_SIZE, 279,
										Short.MAX_VALUE)));

		splitPane = new JSplitPane();
		scrollPane_1.setViewportView(splitPane);
		splitPane.setDividerSize(0);
		valuePanel = new JPanel();
		splitPane.setRightComponent(valuePanel);
		valuePanel.setLayout(new GridLayout(0, 1, 0, 0));

		namePanel = new JPanel();
		splitPane.setLeftComponent(namePanel);
		namePanel.setLayout(new GridLayout(0, 1, 0, 0));
		existedContent.setLayout(gl_existedContent);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						doAlgorithm();

					}

				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");

				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	protected abstract AlgorithmTabPane saveConfig();

	protected void initExistedConfig(Map<String, String> options) {

		Set<Map.Entry<String, String>> optionSet = options.entrySet();
		JLabel lblNewLabel = null;
		for (Map.Entry<String, String> option : optionSet) {
			lblNewLabel = new JLabel(option.getKey());
			namePanel.add(lblNewLabel);
			lblNewLabel = new JLabel(option.getValue());
			lblNewLabel.setBorder(BorderFactory.createEtchedBorder());
			valuePanel.add(lblNewLabel);
		}
	}

	protected String createTabTitle() {
		return toString();
	}

	protected abstract void doAlgorithm();
}
